package leetcode.easy.q13;

import java.util.HashMap;
import java.util.Map;

public class Question {
    public static void main(String[] args) {
        System.out.printf(String.format("result:%s",String.valueOf(new Solution2().romanToInt("XLIX"))));
    }
}

/**
 * solution1: 基于map做映射，
 */
class Solution {
   static Map<Character, Integer> map = new HashMap<>();
    static {
        map.put('I', 1);
        map.put('V', 5);
        map.put('X', 10);
        map.put('L', 50);
        map.put('C', 100);
        map.put('D', 500);
        map.put('M', 1000);
    }
    public int romanToInt(String s) {

        int result = 0, last = 0, flag = 1;
        for (int i = 0; i < s.length(); i++) {
            int current = map.get(s.charAt(i));
            if (last < current) {
                last = -last;
            }
            result = result + last;
            last = current;
        }
        result = result + last;
        return result;

    }
}

/**
 * 基于switch做映射
 */
class Solution2 {
    public int romanToInt(String s) {
        int result = 0, last = 0, flag = 1;
        for (int i = 0; i < s.length(); i++) {
            int current = getVal(s.charAt(i));
            if (last < current) {
                last = -last;
            }
            result = result + last;
            last = current;
        }
        result = result + last;
        return result;

    }

    private int getVal(char c) {
        switch (c) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;

            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                return 0;
        }
    }
}